﻿//-----------------------------------------------------------------------
// <copyright file="Transition.cs" company="Sławomir Rosiek">
//     Copyright (c) Sławomir Rosiek. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Rosieks.Web.Flow.Metadata
{
    using System;
    using System.Web.Mvc;

    [System.Diagnostics.DebuggerDisplay("{On, nq} -> [{To}]")]
    public partial class Transition : IExecutionObject
    {
        private ITransitionCondition conditionObject;
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Transition"/> class.
        /// </summary>
        public Transition()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:Transition"/> class.
        /// </summary>
        public Transition(string on, State to)
        {
            this.On = on;
            this.To = to;
            this.RequireValidModel = true;
        }

        public ITransitionCondition ConditionObject
        {
            get
            {
                if (this.conditionObject == null)
                {
                    this.conditionObject = this.InitializeCondition();
                }

                return this.conditionObject;
            }

            set
            {
                this.conditionObject = value;
            }
        }

        public virtual bool PushOnStack()
        {
            return true;
        }

        public bool IsValid(ControllerContext context)
        {
            return (!this.RequireValidModel || this.IsModelValid(context)) &&
                   (this.ConditionObject == null || this.IsConditionValid(context));
        }

        protected virtual bool IsModelValid(ControllerContext context)
        {
            var controller = context.Controller as Controller;
            if (controller == null)
            {
                // TODO: Add message from resource
                throw new NotSupportedException();
            }
            else
            {
                return controller.ModelState.IsValid;
            }
        }

        protected virtual bool IsConditionValid(ControllerContext context)
        {
            return this.ConditionObject.IsValid(context);
        }

        private ITransitionCondition InitializeCondition()
        {
            if (!string.IsNullOrEmpty(this.Condition))
            {
                Type conditionType = Type.GetType(this.Condition);
                if (conditionType != null)
                {
                    return (ITransitionCondition)DependencyResolver.Current.GetService(conditionType);
                }
                else
                {
                    throw new ArgumentException(string.Format("There is not type '{0}'", this.Condition), "Condition");
                }
            }
            else
            {
                return null;
            }
        }
    }
}
